<?php

if (!defined('SYSPATH')) exit("No direct script access allowed!");

class CL_URI {

    private static $instance = null;
    public $string = '';
    public $segments = array();

    private function CL_URI() {
        $this->fetch_uri_string();
        $this->parse_uri_segments();
    }

    /**
     * @return CL_Uri
     */
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new CL_URI();
        }
        return self::$instance;
    }

    private function fetch_uri_string() {

        $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');

        if (trim($path, '/') != '' && $path != "/".SELF) {
            $this->string = ltrim($path, '/');
            return;
        }
    }

    private function parse_uri_segments() {

        foreach (explode('/', $this->string) as $segment) {
            if (trim($segment) != '') {
                $this->segments[] = $segment;
            }
        }
    }
}


